<!--
 * @Author: your name
 * @Date: 2021-04-06 19:07:23
 * @LastEditTime: 2021-04-12 15:38:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shop\training-group-5-warehouse\dianshang\src\views\Group\index.vue
-->
<!--邀请好友拼团-->
<template>
  <div class="group">
    <header>
      <span @click="()=>$router.push('/layout/home')">&lt;</span>
    </header>
    <main>
      <div v-for="(item, index) in shopGroup" :key="index">
        <Conduct :data="item" @handleOver="handleOver" />
      </div>
      <dl v-for="item in groupList" :key="item.shopId">
        <dt>
          <img :src="item.groupImgUrl" alt />
        </dt>
        <dd>
          <div>
            <span>{{item.title}}</span>
          </div>
          <div>
            <span>{{item.groupNumber}}人团</span>
          </div>
          <div>
            <span>
              <p>
                <b>￥{{item.price}}</b>
                <s>￥{{item.beforePrice}}</s>
              </p>
              <button @click="handlePushDetails(item.shopId)">去拼团 ></button>
            </span>
          </div>
        </dd>
      </dl>
    </main>
    <footer :class="footerActive ? 'position' : 'positions'">
      <div class="footerTop">
        <span @click="handleSetTainer">&lt;</span>
      </div>
      <div class="footerCenter">
        <div class="overall" style="flex: 1;">
          <!-- 步骤条盒子 -->
          <div class="steps-box">
            <!-- 步骤条 -->
            <div class="Article-steps" :class="stepList.length <= activeIndex ? 'step-over' : ''">
              <!-- 步骤条背景进度条 -->
              <div class="line">
                <span
                  class="plan"
                  :style="
                    `width:${activeIndex * (100 / (stepList.length - 1)) -
                      100 / (stepList.length - 1)}%`
                  "
                ></span>
              </div>
              <!-- 每步部分 -->
              <span
                class="step"
                v-for="(i, index) in stepList"
                :key="index"
                :class="
                  activeIndex == i.stepIndex || i.stepIndex <= activeIndex
                    ? 'step-active'
                    : ''
                "
              >
                <span class="step-num">
                  <span
                    class="num"
                    :class="
                      activeIndex == i.stepIndex || i.stepIndex <= activeIndex
                        ? 'active'
                        : ''
                    "
                    v-html="i.stepIndex < activeIndex ? '√' : i.title"
                  ></span>
                </span>
              </span>
            </div>
          </div>
        </div>
        <span class="twoCity">
          {{
          overShow ? "第三步：仓库发货" : "第二步：填地址"
          }}
        </span>
      </div>
      <div class="footerBottom">
        <div class="bottomHeader">
          <span></span>
          <span>{{ overShow ? "仓库正在发货中" : "确认收货地址" }}</span>
          <span @click="handleSetTainer">X</span>
        </div>
        <div class="bottomMain">
          <div class="cityFalse">
            <div class="title" v-if="userCity.length === 0">
              <van-icon name="location" size="1rem" />
              <span>您还没有收货地址</span>
            </div>
            <div
              v-if="userCity.length !== 0"
              style="
                width: 100%;
                height: 120px;
                font-size: 18px;
                display: flex;
               "
            >
              <div
                style="
                width: 60px;
                height: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                "
              >
                <van-icon name="location" size="1rem" style=" color: salmon;" />
              </div>
              <div>
                <div style="flex: 1; height: auto; display: flex;">
                  <span
                    style="font-size: 14px; margin: 0 10px;  font-weight: 600;"
                  >{{ userCity[defaultCityIndex].name }}</span>
                  <span style="font-size: 12px; margin: 0 10px;">
                    {{
                    userCity[defaultCityIndex].phoneNumber
                    }}
                  </span>
                </div>
                <div
                  style="flex: 1; height: auto; display: flex;  flex-direction: column; margin-top: 10px;"
                >
                  <span style="font-size: 14px; margin: 0 10px;">
                    {{
                    userCity[defaultCityIndex].region
                    }}
                  </span>
                  <span style="font-size: 12px; margin: 0 10px;">
                    {{
                    userCity[defaultCityIndex].detailed
                    }}
                  </span>
                </div>
              </div>
            </div>
            <div class="addCity" v-if="overShow === false">
              <span @click="() => (address = true)" v-if="userCity.length === 0">添加收货地址</span>
              <span v-if="userCity.length !== 0" @click="()=>cityIsShow=true">选择其他收货地址</span>
              <van-icon
                name="arrow-down"
                @click="() => userCity.length === 0?address = true:cityIsShow=true"
              />
            </div>
            <div class="addCity" v-if="overShow === true">
              <span>请注意查收</span>
            </div>
          </div>
        </div>
        <div class="bottomFooter">
          <div class="footerTitle">
            <span>推荐相似商品</span>
          </div>
          <div class="footerShops">
            <div v-for="item in recommendList" :key="item.id">
              <img :src="item.imgUrl" alt />
              <span>{{ item.title }}</span>
            </div>
          </div>
          <div class="footerBotton" v-if="overShow === false">
            <van-button
              color="linear-gradient(to right, #ff6034, #ee0a24)"
              style="width: 100%; height: 100%;  border-radius: 20px;"
              @click="handleStart"
            >已选好商品，去结算</van-button>
          </div>
        </div>
      </div>
      <div class="cityForm" v-if="address">
        <div class="formContainer">
          <div class="vanish" @click="() => (address = false)">x</div>
          <div class="content">
            <h2>添加收货地址</h2>
            <van-form @submit="onSubmit">
              <van-field
                v-model="consignee"
                name="consignee"
                label="收货人"
                placeholder="收货人"
                :rules="[{ required: true, message: '请填写收货人' }]"
              />
              <van-field
                v-model="phone"
                name="phone"
                label="手机号"
                placeholder="手机号"
                :rules="[{ required: true, message: '请填写手机号' }]"
              />
              <van-field
                readonly
                clickable
                name="region"
                :value="region"
                label="地区选择"
                placeholder="点击选择省市区"
                @click="showArea = true"
                :rules="[{ required: true, message: '选择省市区' }]"
              />
              <van-field
                v-model="detailed"
                name="detailed"
                label="详细地址"
                placeholder="如街道，门牌号，小区，乡镇，村等"
                :rules="[{ required: true, message: '请填写详细地址' }]"
              />
              <van-popup v-model="showArea" position="bottom">
                <van-area :area-list="areaList" @confirm="onConfirm" @cancel="showArea = false" />
              </van-popup>
              <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">保存</van-button>
              </div>
            </van-form>
          </div>
        </div>
      </div>
    </footer>
    <CityForm
      :isShow="cityIsShow"
      :cityData="userCity"
      :cityIndex="defaultCityIndex"
      @handleSetIsShow="handleNoneCityForm"
      @handleAddCity="handleAddCity"
      @handleDeleteCity="handleDeleteCity"
      @handleCompile="handleCompile"
      @handleMonitor="handleMonitor"
    />
    <Dirlogs
      :isShow="dirlogsShow"
      :type="dirlogType"
      @handleAddCity="handleAddCity"
      @handleCancel="handleCancel"
    />
  </div>
</template>

<script>
import Dirlogs from "./components/Dirlogs"; //复用弹框
import { areaList } from "../../mock/cityData.js"; //城市数据
import CityForm from "./components/CityForm"; //地址页面
import Conduct from "./components/Conduct";
import { mapState, mapMutations } from "vuex";
export default {
  components: {
    Dirlogs,
    CityForm,
    Conduct,
  },
  data() {
    return {
      groupList: [], //获取拼团数据容器
      footerActive: 0, //侧边弹框开关
      routeData: this.$route.query.show,
      shopGroup: [], //商品拼团详情
      //步骤条数据
      stepList: [
        {
          stepIndex: 1,
          title: "1",
        },
        {
          stepIndex: 2,
          title: "发货",
        },
        {
          stepIndex: 3,
          title: "支付",
        },
      ],
      activeIndex: 2, //进度条进度
      overShow: false, //完成状态
      cityItemIndex: 0, //指定地址渲染
      dirlogFlag: false, //免费拿地址是否填写
      recommendList: [], //推荐商品数据
      address: false, //表单弹框开关
      userCityItem: {}, //输入地址表单
      consignee: "", //收货人
      phone: "", //手机号
      region: "", //地区
      detailed: "", //详细地址
      done: false, //免费拿是否完成状态
      showArea: false, //城市选择控制开关
      areaList: areaList, // 城市选择数据
      userCity: [], //收货人地址
      key: 0, //地址id
      dirlogsShow: false, //复用弹框开关
      dirlogType: "addCity", //弹框类型
      cityIsShow: false, //地址表状态
      cityType: "addCity", //地址表单状态类型
      defaultCityIndex: 0, //默认地址下标
    };
  },
  computed: {
    ...mapState("group", ["shopItem"]),
    ...mapState("login", ["userInfo"]),
  },
  created() {
    this.getGroupList(); //获取拼团数据
    this.handleGetRecommendData(); //获取推荐商品数据
    this.handleGetCitys(); //获取收货地址
    this.handleGetShopDetails(); //获取拼团详情
  },
  methods: {
    ...mapMutations("group", ["SET_SHOPITEM"]),
    //获取拼团数据
    async getGroupList() {
      const data = await this.$api.group();
      if (data.code === 0) {
        this.groupList = data.data.data;
      }
    },
    //跳转详情页
    handlePushDetails(shopId) {
      localStorage.setItem("shopId", shopId);
      this.$api.groupItem(shopId).then((res) => {
        if (res.code === 0) {
          this.SET_SHOPITEM(res.data.data[0]);
        }
      });
      this.$router.push({ path: "/details", query: { id: shopId } });
    },
    //关闭侧边栏
    handleSetTainer() {
      this.footerActive = false;
      this.activeIndex = 2;
    },
    //获取推荐商品数据
    handleGetRecommendData() {
      this.$api.recommend().then((res) => {
        if (res.code === 200) {
          this.recommendList = res.data.recommend;
        }
      });
    },
    //已选好商品，去结算
    handleStart() {
      this.dirlogsShow = true;
      if (this.userCity.length === 0) {
        this.dirlogType = "addCity";
      } else {
        this.dirlogType = "goldKnife";
      }
    },
    //城市选择事件
    onConfirm(values) {
      this.region = values
        .filter((item) => item)
        .map((item) => item.name)
        .join("/");
      this.showArea = false;
    },
    handleCancel() {
      this.dirlogsShow = false; //Dirlogs弹框隐藏
    },
    //隐藏地址表页面
    handleNoneCityForm() {
      this.cityIsShow = false;
    },
    //获取收货地址
    async handleGetCitys() {
      const data = await this.$api.fandcitylist();
      if (data.code === 0) {
        this.userCity = data.data.data;
      } else {
        this.userCity = [];
      }
    },
    //删除地址
    handleDeleteCity({ id, cityIndex }) {
      if (this.defaultCityIndex === cityIndex) {
        this.defaultCityIndex = 0;
      }
      this.$api.deletecitylist(id).then((res) => {
        if (res.code === 0) {
          alert("删除成功");
          this.handleGetCitys();
        }
      });
    },
    //编辑地址
    handleCompile(values) {
      this.cityType = "compileCity";
      this.dirlogsShow = false;
      this.address = true;
      this.consignee = values.name;
      this.phone = values.phoneNumber;
      this.region = values.region;
      this.detailed = values.detailed;
      this.key = values.id;
      this.handleNoneCityForm();
    },
    //添加地址
    handleAddCity() {
      this.cityType = "addCity";
      this.dirlogsShow = false;
      this.address = true;
      this.consignee = "";
      this.phone = "";
      this.region = "";
      this.detailed = "";
      this.handleNoneCityForm();
    },
    //修改默认选中状态
    handleMonitor(cityIndex) {
      this.defaultCityIndex = cityIndex;
    },
    //表单提交按钮
    onSubmit(values) {
      this.userCityItem = {
        name: values.consignee,
        phoneNumber: values.phone,
        region: values.region,
        detailed: values.detailed,
      };
      if (this.cityType === "addCity") {
        //添加地址
        this.$api.addcitylist(this.userCityItem).then((res) => {
          if (res.code === 0) {
            this.address = false;
            this.cityIsShow = true;
            this.handleGetCitys();
          }
        });
      } else {
        //修改地址
        const id = this.key;
        this.$api.updatecitylist(this.userCityItem, id).then((res) => {
          if (res.code === 0) {
            this.address = false;
            this.cityIsShow = true;
            this.handleGetCitys();
          }
        });
      }
    },
    //获取拼团详情
    async handleGetShopDetails() {
      const userId = this.userInfo.user;
      const data = await this.$api.fandGroupdetails(userId);
      if (data.code === 0) {
        this.shopGroup = data.data.data;
      }
    },
    //完成拼团
    handleOver() {
      this.footerActive = true;
      this.activeIndex = 3;
      this.overShow = true;
      this.$api.recommend().then((res) => {
        if (res.code === 200) {
          this.recommendList = res.data.recommend;
        }
      });
    },
  },
  //监听路由改变侧边栏的状态
  beforeRouteEnter: (to, from, next) => {
    next((vm) => {
      if (from.path === "/details") {
        vm.footerActive = Number(vm.routeData);
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.group {
  width: 100%;
  height: 100%;
  background: url("../../assets/img/group BookingBackground.jpg");
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  header {
    width: 100%;
    height: 249px;
    display: flex;
    justify-content: space-between;
    span {
      color: #fff;
      padding: 0 20px;
    }
  }
  main {
    flex: 1;
    overflow-y: scroll;
    display: flex;
    flex-direction: column;
    align-items: center;
    dl {
      width: 90%;
      height: auto;
      background: #fff;
      margin: 5px 0;
      display: flex;
      align-items: center;
      padding: 10px 0;
      border-radius: 5px;
      dt {
        width: 100px;
        height: 100px;
        margin: 0 10px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      dd {
        flex: 1;
        div {
          width: 100%;
          padding: 2px 0;
          display: flex;
          font-size: 12px;
        }
        div:nth-child(1) {
          font-weight: 600;
          color: #4f4d4e;
        }
        div:nth-child(2) {
          color: #d8724a;
          span {
            font-weight: 600;
            padding: 0 2px;
            border: solid 1px #d8724a;
            margin: 2px 0;
          }
        }
        div:nth-child(3) {
          p {
            display: inline-block;
            width: 150px;
            s {
              margin: 0 2px;
              text-decoration: line-through;
              color: #9f9ea0;
            }
            b {
              font-size: 16px;
            }
          }
          button {
            background: #ea3628;
            border: none;
            outline: none;
            color: #fcebdc;
            width: 65px;
            height: 25px;
          }
        }
      }
    }
  }
  footer {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    position: fixed;
    left: 100%;
    top: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: 1s;
    .footerTop {
      width: 100%;
      height: 46px;
      display: flex;
      align-items: center;
      padding: 0 10px;
      span {
        color: rgb(155, 152, 152);
      }
    }
    .footerCenter {
      width: 100%;
      height: 200px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .twoCity {
        width: 100%;
        height: 100px;
        font-size: 18px;
        color: #eee990;
        margin-top: 10px;
        font-weight: 600;
        display: flex;
        justify-content: center;
      }
      .steps-box {
        user-select: none;
        width: 100%;
        height: 100%;
        position: relative;
        margin-top: 20px;
        // <!-- 步骤条背景进度条 -->
        .line {
          display: block;
          margin: 0 auto;
          position: absolute;
          top: 24px;
          left: 5%;
          background: #ccc;
          width: 90%;
          height: 2px;
          overflow: hidden;
          .plan {
            position: absolute;
            top: 0;
            left: 0;
            height: 2px;
            transition: 3s;
            background: #8ab4f3;
          }
        }
        .Article-steps {
          display: flex;
          justify-content: space-between;
          .step {
            .step-num {
              width: 50px;
              height: 50px;
              display: inline-block;
              line-height: 50px;
              background: #c0c0c0;
              clip-path: polygon(50% 0, 100% 85%, 0 85%);
              color: white;
              font-weight: bold;
              transition: 3s;
              font-size: 14px;
              position: relative;
              .num {
                transition: 3s;
                display: inline-block;
                width: 25px;
                height: 10px;
                transition: 3s;
                display: inline-block;
                position: absolute;
                left: 13px;
                top: 4px;
                font-size: 12px;
              }
              .num.active {
                transform: rotate(-90deg);
                left: -6px;
                top: 14px;
                font-weight: 600;
              }
            }
          }
        }

        //当前所在位置样式
        .step-active {
          .step-num {
            background: #2d7df5 !important;
            transform: rotate(90deg);
          }
        }

        //全部完成样式
        .step-over {
          .plan {
            background: #91f062 !important;
          }
          .step-num {
            background: #67c23a !important;
          }
        }
      }
    }
    .footerBottom {
      flex: 1;
      background: #fff;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      background: #f4f4f4;
      .bottomHeader {
        width: 100%;
        height: 46px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 10px;
        background: #fff;
        span {
          font-size: 14px;
          color: #989898;
          font-weight: 600;
        }
        span:nth-child(2) {
          color: #242424;
        }
      }
      .bottomMain {
        flex: 1;
        background: #fff;
        margin-bottom: 20px;
        .cityFalse {
          width: 100%;
          height: 100%;
          display: flex;
          flex-direction: column;
          .title {
            width: 100%;
            height: 120px;
            font-size: 18px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: salmon;
            font-weight: 600;
          }
          .addCity {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            padding: 0 10px;
          }
        }
      }
      .bottomFooter {
        width: 100%;
        height: 200px;
        background: #fff;
        display: flex;
        flex-direction: column;
        .footerTitle {
          width: 100%;
          height: 46px;
          display: flex;
          align-items: center;
          justify-content: center;
          span {
            font-size: 14px;
            font-weight: 600;
          }
        }
        .footerShops {
          flex: 1;
          display: flex;
          align-items: center;
          overflow-x: scroll;
          padding: 0 10px;
          div {
            width: 100px;
            height: 80%;
            margin: 0 10px;
            flex-shrink: 0;
            position: relative;
            img {
              width: 100%;
              height: 100%;
            }
            span {
              width: 100%;
              height: 30px;
              color: rgb(231, 219, 219);
              background: rgba(0, 0, 0, 0.4);
              position: absolute;
              left: 0;
              bottom: 0;
              font-size: 12px;
              text-align: center;
              line-height: 30px;
            }
          }
        }
        .footerBotton {
          width: 100%;
          height: 46px;
          padding: 0 40px;
          display: flex;
          align-items: center;
        }
      }
    }
  }
}
footer.position {
  left: 0;
}
footer.positions {
  left: 100%;
}
.cityForm {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.8);
  padding: 120px 30px;
  .formContainer {
    width: 100%;
    height: 100%;
    background: #fff;
    position: relative;
    border-radius: 10px;
    .content {
      width: 100%;
      height: 100%;
      h2 {
        width: 100%;
        height: 46px;
        font-size: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .vanish {
    width: 30px;
    height: 30px;
    background: #ccc;
    border-radius: 50%;
    text-align: center;
    line-height: 30px;
    position: absolute;
    right: -12px;
    top: -20px;
    font-size: 20px;
  }
}
.van-icon-location {
  color: salmon !important;
}
</style>